﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Audio;
using Spinning.Spinning.Unitity;

namespace Spinning.Spinning
{
    class Music
    {
        SoundEffect _song;
        SoundEffectInstance _songInstance;
        float _fadeInterval;

        public float Volume
        {
            set { _songInstance.Volume = value; }
            get { return _songInstance.Volume; }
        }

        //start with full volumn.
        public Music(String FromUrl, float interval, bool looped)
        {
            String tempUrl = ResourceManager.ContentRoot;
            ResourceManager.ContentRoot = "Music";
            _song = ResourceManager.LoadSoundEffect(FromUrl);
            _songInstance = _song.CreateInstance();
            _songInstance.IsLooped = looped;
            _songInstance.Volume = 1.0f;
            _fadeInterval = interval;
            ResourceManager.ContentRoot = tempUrl;
        }

        //choose the volumn by yourself.
        public Music(String FromUrl, float interval, float volumn)
        {
            String tempUrl = ResourceManager.ContentRoot;
            ResourceManager.ContentRoot = "Music";
            _song = ResourceManager.LoadSoundEffect(FromUrl);
            _songInstance = _song.CreateInstance();
            _songInstance.IsLooped = true;
            _songInstance.Volume = volumn;
            _fadeInterval = interval;
            ResourceManager.ContentRoot = tempUrl;
        }

        //function to play.
        public void MusicPlay()
        {
            _songInstance.Play();
        }
        //function to pause.
        public void MusicPause()
        {
            _songInstance.Pause();
        }
        //function to stop.
        public void MusicStop()
        {
            _songInstance.Stop();
            //_songInstance.
        }
        //function to free the memory space.
        public void MusicFree()
        {
            _songInstance.Stop();
            _songInstance.Dispose();
            while (!_songInstance.IsDisposed) { }
        }
        
        //Set the repeat bool value.
        public bool Repeat
        {
            set
            {
                _songInstance.IsLooped = value;
            }
            get
            {
                return _songInstance.IsLooped;
            }
        }
    }
}
